﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;

using mshtml;

namespace PasswordProvider.ViewModel.Helpers
{
    class IDataObjectHelper
    {
        IDataObject _dropData;
        public IDataObjectHelper(IDataObject dropData)
        {
            _dropData = dropData;
        }

        public bool HasUrl
        {
            get
            {
                if (_dropData.GetDataPresent("UniformResourceLocator"))
                    return true;
                if (_dropData.GetDataPresent("Text"))
                {
                    string test = (string)_dropData.GetData("Text");
                    if (Uri.IsWellFormedUriString(test, UriKind.Absolute))
                        return true;
                    if (test.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
                        return true;
                }
                return false;
            }
        }

        /// <summary>
        ///  Get the url contained in the drop data
        /// </summary>
        public Uri Url
        {
            get
            {
                string url = (string)_dropData.GetData("Text");
                if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
                    return UriHelper.CreateUri(url);
                return null;

                //using (MemoryStream urlStream = (MemoryStream)_eventArgs.Data.GetData("UniformResourceLocator"))
                //{
                //    using (StreamReader reader = new StreamReader(urlStream))
                //    {
                //        url = reader.ReadToEnd();
                //    }
                //}
                //url = url.Trim();
                
            }
        }

        /// <summary>
        /// Get the Icon url of the web site at Url
        /// </summary>
        public Uri IconUrl
        {
            get
            {
                HTMLDocument doc = IENavigationHelper.OpenUrl(Url);
                if (doc != null)
                {
                    return IEHtmlDocumentHelper.FindFavIcon(doc);                    
                }

                return null;
            }
        }

        public string Title
        {
            get
            {
                if (_dropData.GetDataPresent("FileGroupDescriptor"))
                {
                    Stream theStream = (Stream)_dropData.GetData("FileGroupDescriptor");

                    byte[] fileGroupDescriptor = new byte[512];
                    theStream.Read(fileGroupDescriptor, 0, 512);

                    StringBuilder fileName = new StringBuilder("");

                    // file name begins at location 76
                    for (int i = 76; fileGroupDescriptor[i] != 0; i++)
                    {
                        fileName.Append(Convert.ToChar(fileGroupDescriptor[i]));
                    }
                    theStream.Close();

                    string title = fileName.ToString();
                    title = Path.GetFileNameWithoutExtension(title);

                    return title;
                }
                else
                {
                    return null;
                }
            }
        }
        
    }
}
